/**
 * @Author wonghiu
 * @Date 2021/7/19 16:31
 * @Version 1.0.1
 */
public class checkPartition {
    public static void main(String[] args) {
//        int[] nums = new int[]{5,7,7,8,8,10};
        int[] nums = new int[]{1,2,3,4,5,6};
        int target = 8;

//        System.out.println(m1(nums,target));
//        System.out.println(m2(nums,target));
//        System.out.println(m3(nums,target));
//        System.out.println(m4(nums));
        System.out.println(m5(nums));
        System.out.println(m6(nums));
    }
    private static int m6(int[] nums) {
        int l = 0;
        int r = nums.length - 1;
        while (l < r) {
            int mid = l + (r - l)+1 / 2;
            if (nums[mid] >= nums[0]) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        return l;
    }
    private static int m5(int[] nums) {
        int l = 0;
        int r = nums.length - 1;
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] <= nums[nums.length-1]) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }

    // 找旋转点
    private static int m4(int[] nums) {
        int l = 1;
        int r = nums.length - 1;
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] < nums[r]) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }

    // 最左匹配
    private static int m1(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] >= target) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        if (nums[l] == target) {
            return l;
        } else {
            return -1;
        }
    }

    // 最右匹配
    private static int m2(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;
        while (l < r) {
            int mid = l + (r - l)+1 / 2;
            if (nums[mid]<=target){
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        if (nums[l] == target) {
            return l;
        } else {
            return -1;
        }
    }

    // 最左匹配，如果匹配不到，返回前一个
    private static int m3(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (nums[mid] == target) {
                r = mid;
            } else if (nums[mid] < target) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        if (nums[l] == target) {
            return l;
        } else {
            return Math.max(0, l - 1);
        }
    }
}
